#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File        :   面试题 02.07. 链表相交.py    
@Contact     :   lhouctcs@163.com
@License     :   MIT
@Time        :   2023/2/4 14:22
@Author      :   lhou 
@Version     :   0.1.0
@Description :
"""


# import lib
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        if not headA or not headB:
            return None
        a = headA
        b = headB
        while a != b:
            a = a.next if a else headB
            b = b.next if b else headA
        return a
